﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Xml.XPath;
using Jianshu_Research_Tools;
using Newtonsoft.Json;

namespace Jianshu_Research_Tools
{
    public class BasicApis
    {
        /// <summary>
        /// 通用静态HttpGetMethod函数。
        /// </summary>
        /// <param name="Url">传入的UTL地址</param>
        /// <returns>返回的结果。</returns>
        protected static object StaticGet(string Url)
        {
            if (Url == null)
            {
                throw new ArgumentNullException(nameof(Url));
            }

            HttpWebRequest req = WebRequest.CreateHttp(Url);
            req.Method = "GET";
            //req.Headers = Headers.Jianshu_request_header;
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream resStream = resp.GetResponseStream();
            StreamReader reader = new StreamReader(resStream, Encoding.UTF8);
            string result = reader.ReadToEnd();
            reader.Close();

            return result;
        }
        /// <summary>
        /// 获取文章的Json信息
        /// </summary>
        /// <param name="article_url">文章的Url</param>
        /// <returns>获取的文章信息</returns>
        public static string GetArticleJsonDataApi(string article_url)
        {
            string request_url = article_url.Replace("https://www.jianshu.com/", "https://www.jianshu.com/asimov/");
            AssertFuncs.AssertArticleUrl(request_url);       
            string result = StaticGet(Url: request_url).ToString();
            return result;
        }

        /// <summary>
        /// 根据文章ID获取文章评论的Json信息。
        /// </summary>
        /// <param name="article_id">文章ID</param>
        /// <param name="page">评论的第page页</param>
        /// <param name="count">获取评论的数量</param>
        /// <param name="author_only">是否只获取作者及其回复的评论</param>
        /// <param name="order_by">指定某用户名</param>
        /// <returns>获取的文章评论信息。</returns>
        public static string GetArticleCommentsJsonDataApi(
            int article_id,int page=1,int count=1,
            bool author_only=false,string order_by=""
            )
        {
            string request_url = $"https://www.jianshu.com/shakespeare/notes/{article_id}" +
                $"/comments?page={page}&count={count}&author_only={author_only}" +
                $"&order_by={order_by}";
            string result = StaticGet(request_url).ToString();
            return result;
        }

        /// <summary>
        /// 获取文章的底部推荐信息。
        /// </summary>
        /// <param name="article_url">要查询的文章Url</param>
        /// <returns>获取的文章底部推荐信息。</returns>
        public static string GetArticleHtmlJsonDataApi(string article_url)
        {
            string request_url = article_url;
            string result = StaticGet(request_url).ToString();
            string json = result.Split(new[] { "type=\"application/json\">" },
                StringSplitOptions.None)[1].Split(new[] { "</script><script nomodule=\"\"" }
                ,StringSplitOptions.None)[0];
            return json;
        }

        /// <summary>
        /// 获取简书文集信息。
        /// </summary>
        /// <param name="collection_url">要查询的文集Url</param>
        /// <returns>获取的文集信息。</returns>
        public static string GetCollectionJsonDataApi(string collection_url)
        {
            string request_url = collection_url.Replace("https://www.jianshu.com/c/",
                "https://www.jianshu.com/asimov/collections/slug/");
            string result = StaticGet(request_url).ToString();
            return result;
        }

        /// <summary>
        /// 获取文集的文章信息。
        /// </summary>
        /// <param name="cid">文集的ID</param>
        /// <param name="page">获取的页数</param>
        /// <returns>获取的文集文章信息。</returns>
        public static string GetCollectionEditorsJsonDataApi(int cid,int page)
        {
            string request_url = $"https://www.jianshu.com/collections/{cid}" +
                $"?page={page}";
            string result = StaticGet(request_url).ToString();
            return result;
        }

        /// <summary>
        /// 获取文集的推荐作家信息。
        /// </summary>
        /// <param name="cid">文集的ID</param>
        /// <param name="page">获取的页数</param>
        /// <param name="count">推荐作家的数量</param>
        /// <returns>获取的文集的推荐作家信息。</returns>
        public static string GetCollectionRecommendedWritersJsonDataApi(
            int cid,int page,int count
            )
        {
            string request_url = $"https://www.jianshu.com/collections/recommended_users" +
                $"?collection_id={cid}&page={page}&count={count}";
            string result = StaticGet(request_url).ToString();
            return result;
        }

        /// <summary>
        /// 获取文集的订阅者信息。
        /// </summary>
        /// <param name="cid">文集的ID</param>
        /// <param name="max_sort_id">排序的用户ID</param>
        /// <returns>获取的文集的订阅者信息。</returns>
        public static string GetCollectionSubscribersJsonDataApi(
            int cid,int max_sort_id
            )
        {
            string request_url = $"https://www.jianshu.com/collection/{cid}/subscribers" +
                $"?max_sort_id={max_sort_id}";
            string result = StaticGet(request_url).ToString();
            return result;
        }

        /// <summary>
        /// 获取文集中的文章信息。
        /// </summary>
        /// <param name="collection_slug">文集简书中的的Slug</param>
        /// <param name="page">获取的页数</param>
        /// <param name="count">获取的文章数量</param>
        /// <param name="order_by">查询目标</param>
        /// <returns>获取的文集中的文章信息。</returns>
        public static string GetCollectionArticlesJsonDataApi(
            string collection_slug,int page,int count,string order_by
            )
        {
            string request_url = $"https://www.jianshu.com/asimov/collections/slug/" +
                $"{collection_slug}/public_notes" +
                $"?page={page},count={count}&order_by={order_by}";
            string result = StaticGet(request_url).ToString();
            return result;
        }

        /// <summary>
        /// 获取小岛信息。
        /// </summary>
        /// <param name="island_url">小岛的Url</param>
        /// <returns>获取的小岛信息。</returns>
        public static string GetIslandJsonDataApi(string island_url)
        {
            string request_url = island_url;
            string result = StaticGet(request_url).ToString();
            return result;
        }

        /// <summary>
        /// 获取小岛文章信息。
        /// </summary>
        /// <param name="group_slug">组标识符</param>
        /// <param name="max_id">最大id</param>
        /// <param name="count">获取数量</param>
        /// <param name="order_by">查询目标</param>
        /// <returns></returns>
        public static string GetIslandPostsJsonDataApi(
            string group_slug, int max_id,int count, string order_by
            )
        {
            string request_url = $"https://www.jianshu.com/asimov/posts" +
                $"?group_slug={group_slug}&max_id={max_id}" +
                $"&count={count}&order_by={order_by}";
            string result = StaticGet(request_url).ToString();
            return result;
        }


    }
}
